
package chatclient.applets;

import chatclient.applets.canvas.ListCanvas;
import chatclient.applets.components.Button;
import chatclient.applets.components.Scrollbar;
import chatclient.netio.Member;
import chatclient.netio.Perms;
import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JPanel;


public class ListPanel extends ChatPanel implements ActionListener {
 
    private ListCanvas canvas;
    private Adjustable myBar;
    private JPanel userPanel;
    private JPanel permPanel;
    private Vector permButtons;
    private Vector permProfiles;
    private JPanel kickPanel;
    private Button kickButton;
    private Button banButton;
    private JPanel basePanel;
    private JPanel basePanel1;
    private Button ignoreButton;
    private Button privateButton;
    private String extra;
    private Button extraButton;
    private Button searchButton;    
        
     
    public ListPanel(ListView parent)
    {
        super(parent);
    }
    
    @Override
    protected void initChatView() {
       
        super.initChatView();
       
        backgroundInfoNr = 3;
       
        myBar = parent.createScrollbar();
        add((JComponent) myBar, "East");
        canvas = new ListCanvas((ListView)parent, parent, "list", true);
        canvas.setBackground(getBackground());
        canvas.setForeground(getForeground());
        canvas.setFont(getFont());
        add(canvas, "Center");
        if (parent.getParameter("nobuttons") == null) {
            extra = parent.getParameter("extra");
            basePanel1 = new JPanel();
            basePanel1.setBackground(getBackground());
            basePanel1.setLayout(new GridLayout(1, extra != null ? 3 : 2));
            basePanel1.add(ignoreButton = parent.createButton("ignore"));
            basePanel1.add(privateButton = parent.createButton("private"));
            if (extra != null) {
                basePanel1.add(extraButton = parent.createButton(extra));
            }
            basePanel = new JPanel();
            basePanel.setBackground(getBackground());
            basePanel.setLayout(new BorderLayout());
            basePanel.add(searchButton = parent.createButton("search"), "South");
            basePanel.add(basePanel1, "North");
        }
        kickPanel = new JPanel();
        kickPanel.setBackground(getBackground());
        kickPanel.setLayout(new GridLayout(1, 2));
        kickPanel.add(kickButton = parent.createButton("kick"));
        kickPanel.add(banButton = parent.createButton("ban"));
        permPanel = new JPanel();
        permPanel.setBackground(getBackground());
        permButtons = new Vector();
        permProfiles = new Vector();
        String s;
        for (int i = 0; (s = parent.getParameter((new StringBuilder("perm")).append(i).toString())) != null; i++) {
            int j = s.indexOf(':');
            if (j >= 0 && j < s.length() - 1) {
                permButtons.addElement(parent.createButton(s.substring(j + 1)));
                permProfiles.addElement(s.substring(0, j));
            }
        }

        if (permButtons.size() > 0) {
            permPanel.setLayout(new GridLayout(1, permButtons.size()));
            for (int k = 0; k < permButtons.size(); k++) {
                permPanel.add((Button) permButtons.elementAt(k));
            }
        }
        if (ignoreButton != null) {
            ignoreButton.addActionListener(this);
        }
        if (privateButton != null) {
            privateButton.addActionListener(this);
        }
        if (extraButton != null) {
            extraButton.addActionListener(this);
        }
        if (searchButton != null) {
            searchButton.addActionListener(this);
        }
        if (kickButton != null) {
            kickButton.addActionListener(this);
        }
        if (banButton != null) {
            banButton.addActionListener(this);
        }
        String s1;
        for (int l = 0; (s1 = parent.getParameter((new StringBuilder("perm")).append(l).toString())) != null; l++) {
            ((Button) permButtons.elementAt(l)).addActionListener(this);
        }

        userPanel = new JPanel();
        userPanel.setBackground(getBackground());
        add(userPanel, "South");
        updateBar();
        myBar.addAdjustmentListener(canvas);
    }
    
    
    @Override
    protected void cleanupChatView() {
        if (myBar != null && (myBar instanceof Scrollbar)) {
            myBar.removeAdjustmentListener(canvas);
            ((Scrollbar) myBar).destroy();
            myBar = null;
        }
        if (canvas != null) {
            canvas.destroy();
        }
        
        super.cleanupChatView();
    }    

    protected void layoutUserPanel() {

        System.out.println("ADZ - ListPanel.java->layoutUserPanel");
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        System.out.println("ADZ - Caller[class="+stackTraceElements[stackTraceElements.length - 1].getClassName()+" - method="+stackTraceElements[stackTraceElements.length - 1].getMethodName());
        userPanel.removeAll();
        userPanel.setLayout(new BorderLayout());
        String myName = parent.getMyName();
        if (myName != null) {
            Member member = parent.getMember(myName);
            Perms perms;
            if (member != null) {
                perms = member.getPerms();
            } else {
                perms = parent.getChat().getMyChannelPerms();
            }
            if (perms == null) {
                perms = parent.getChat().getMyPerms();
            }
            if (perms == null) {
                perms = new Perms();
            }
            
            System.out.println("ADZ - ListPanel.java->layoutUserPanel:perms.get(Perms.OPERATOR)="+perms.get(Perms.OPERATOR));
            boolean permOperator = perms.get(Perms.OPERATOR);
            boolean permKick = perms.get(Perms.KICK);
            boolean permBan = perms.get(Perms.BAN);
            //boolean permOperator = false;
            //boolean permKick = false;
            //boolean permBan = false;
            
            
            if (permOperator) {
                userPanel.add(permPanel, "North");
            }
            if (permKick || permBan) {
                userPanel.add(kickPanel, "Center");
            }
            if (basePanel != null) {
                userPanel.add(basePanel, "South");
            }
        }
        updateUserPanel(parent.getChat().getSelectedNick());

        userPanel.revalidate();
    }

    protected void updateUserPanel(String s1) {
    }

    protected void updateBar() {
        int i = canvas.getTop();
        int j = canvas.getVisible();
        int k = canvas.getAverageNoteHeight();
        myBar.setMinimum(canvas.getMinimum());
        myBar.setMaximum(canvas.getMaximum());
        myBar.setVisibleAmount(j);
        myBar.setValue(i);
        myBar.setBlockIncrement(j);
        myBar.setUnitIncrement(k);
    }
    
    /*
    @Override
    public void repaint() {
        repaint();
        if (canvas != null) {
            canvas.repaint();
        }
    }
    */    

    @Override
    public void actionPerformed(ActionEvent actionevent) {
        Object obj = actionevent.getSource();
        Chat chat = parent.getChat();
        if (obj == searchButton) {
            chat.onevent("search");
        }
        String s = chat.getSelectedNick();
        if (s == null) {
            return;
        }
        if (obj == ignoreButton) {
            chat.setIgnoreNick(s, !chat.isIgnoreNick(s));
        } else if (obj == privateButton) {
            chat.startPrivate(s);
        } else if (extra != null && obj == extraButton) {
            Member member = parent.getMember(s);
            chat.doInternalEvent(extra, member != null ? member.getAttrs() : null);
        } else if (obj == kickButton) {
            chat.kick(parent.getMyChannel(), s);
        } else if (obj == banButton) {
            chat.ban(parent.getMyChannel(), s);
        } else {
            for (int i = 0; i < permButtons.size(); i++) {
                if (obj == (Button) permButtons.elementAt(i)) {
                    String s1 = (String) permProfiles.elementAt(i);
                    chat.setUserProfile(parent.getMyChannel(), s, s1);
                }
            }

        }
    }

    public void componentResized(ComponentEvent componentevent) {
        updateBar();
    }
    
 
    public ListCanvas getListCanvas()
    {
        return canvas;
    }
    
    
}
